<?php
class TablePagination extends Claycode_service
{
	private $i_offset;
	private $i_maxShown;
	private $arr_currPageObjects;
	private $arr_request;
	
	public function runService($arr_request)
	{
		if(isset($arr_request) && !empty($arr_request)){
			$this->arr_request = $arr_request;
		}
		
		/**
		  "sEcho": intval($arr_request['sEcho']),
		  "iTotalRecords": "57",
		  "iTotalDisplayRecords": "57",
		  "aaData": [
		 */
	}
	
	public function doArticlesPagination($arr_request)
	{
		//pagination
		if(isset($arr_request['iDisplayStart']) && isset($arr_request['iDisplayLength'])){
			//offset of displayed records
			$this->setOffset($arr_request['iDisplayStart']);
				
			//max amount of records to display
			$this->setMaxShown($arr_request['iDisplayLength']);
			
			//order by, sorting
			$this->arr_currPageObjects = $this->o_em->getRepository('Article')->getArticlesByPage(
				$this->getOffset(), 
				$this->getMaxShown(),
				$arr_request['iSortCol_0'],
				$arr_request['sSortDir_0'],
				intval($arr_request['sEcho'])
			);
			fw_util_outputJson($this->arr_currPageObjects);
		}
	
	}
	
	private function setOffset($i_offset = 0)
	{
		if(trim($i_offset) != ''){
			if(is_numeric($i_offset)){
				$this->i_offset = (int)htmlspecialchars($i_offset);
			}
		}
	}
	
	private function setMaxShown($i_maxShown = 10)
	{
		if(trim($i_maxShown) != ''){
			if(is_numeric($i_maxShown)){
				$this->i_maxShown = (int)htmlspecialchars($i_maxShown);
			}
		}
	}
	
	private function getOffset()
	{
		return $this->i_offset;
	}	
	
	private function getMaxShown()
	{
		return $this->i_maxShown;
	}
	
}